package jaux.tank.api.controller.system;

import java.util.Date;
import java.util.Optional;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import jaux.tank.admin.core.base.controller.BaseController;
import jaux.tank.bean.annotion.core.BussinessLog;
import jaux.tank.bean.constant.factory.PageFactory;
import jaux.tank.bean.core.ShiroUser;
import jaux.tank.bean.dictmap.NoticeMap;
import jaux.tank.bean.dto.NoticeDto;
import jaux.tank.bean.dto.NoticeQuery;
import jaux.tank.bean.dto.PageRequestModel;
import jaux.tank.bean.entity.system.Notice;
import jaux.tank.bean.enumeration.BizExceptionEnum;
import jaux.tank.bean.exception.TankException;
import jaux.tank.bean.vo.front.Ret;
import jaux.tank.bean.vo.front.Rets;
import jaux.tank.dao.system.SysNoticeRepository;
import jaux.tank.service.system.LogObjectHolder;
import jaux.tank.service.system.NoticeService;
import jaux.tank.service.system.impl.ConstantFactory;
import jaux.tank.shiro.ShiroKit;
import jaux.tank.utils.ToolUtil;
import jaux.tank.utils.factory.Page;

/**
 * 通知控制器
 *
 * @Date 2017-05-09 23:02:21
 */
@RestController("noticeApi")
@RequestMapping("/api/notice")
public class NoticeController extends BaseController {


    @Resource
    private SysNoticeRepository sysNoticeRepository;

    private final NoticeService noticeService;
    
    
    public NoticeController(NoticeService noticeService) {
		this.noticeService = noticeService;
	}



    /**
     * 获取通知列表
     */
    @GetMapping(value = "/query")
    public Ret<Page<Notice>> list(NoticeQuery query
    		, @ModelAttribute(binding = false) PageRequestModel pageRequest) {
    	ShiroUser user = ShiroKit.getApiRequestUser().orElse(null);
    	if(user == null) {
	    	return Rets.failure("获取用户信息失败");
    	}
    	Page<Notice> notices = noticeService.queryUserNotices(user.getId(), query, PageFactory.getRequestPage());
		return Rets.success(notices);
    }

    /**
     * 新增通知
     */
    @PostMapping(value = "/add")
    @BussinessLog(value = "新增通知",key = "title",dict = NoticeMap.class)
    public Ret<NoticeDto> add(NoticeDto notice) {
        if (ToolUtil.isOneEmpty(notice, notice.getTitle(), notice.getContent())) {
            throw new TankException(BizExceptionEnum.REQUEST_NULL);
        }
        Optional<ShiroUser> user = ShiroKit.getApiRequestUser();
		notice.setCreater(user.isPresent()?user.get().getId():"");
        notice.setCreatetime(new Date());
       notice =sysNoticeRepository.save(notice);
        return Rets.success(notice);
    }

    /**
     * 删除通知
     */
    @DeleteMapping(value = "/delete")
    @BussinessLog(value = "删除通知",key = "noticeId",dict = NoticeMap.class)
    public Ret<String> delete(@RequestParam String noticeId) {

        //缓存通知名称
        LogObjectHolder.me().set(ConstantFactory.me().getNoticeTitle(noticeId));

        this.sysNoticeRepository.deleteById(noticeId);

        return Rets.success();
    }


}
